import sys
import logging

from rag import search_doc 
from reason import generate_ai_call 


SIMILARITY_LEVEL = 0.003
logger = logging.getLogger(__name__)

def process_flow(question):
    logger.info(f"你的问题是: {question}")
    logger.info("正在分析")
    logger.info("针对你的提问，我们的系统可以提供如下支持")

    params_result = None

    doc_content, api_content = search_doc(question)


    # 根据api文档中的内容来生成调用信息
    call_info = f"# 业务描述:\n{doc_content}\n\n# API描述:\n{api_content}"

    logger.info(f"call_info:\n{call_info}")
    call_script = generate_ai_call(call_info)

    logger.info(f"call_script:\n{call_script}")
    return call_script


def main():
    # 创建一个文件处理器
    file_handler = logging.FileHandler("ai_reason_flow.log")
    file_handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(module)s - %(message)s"))

    # 创建一个控制台处理器
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)
    console_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(module)s - %(message)s"))
    logging.basicConfig(
        level=logging.DEBUG,
        force=True,
        handlers=[
            file_handler, console_handler
        ]
    )

    logger.info("脚本开始执行")

    if len(sys.argv) < 2:
        logger.error("请提供你的问题。")
        return

    first_arg = sys.argv[1]
    process_flow(first_arg)


if __name__ == "__main__":
    main()
